<template>
  <div class="app flex-row align-items-center">
    <div class="container">
      <div class="row justify-content-center">
        <div class="col-md-8">
          <div class="card-group mb-0">
            <div class="card p-4">
              <div class="card-body">
                <h1>登录</h1>
                <p class="text-muted">请登录账户</p>
                <div class="input-group mb-3">
                  <span class="input-group-addon"><i class="icon-user"></i></span>
                  <input type="text" class="form-control" placeholder="用户名" v-model="name">
                </div>
                <div class="input-group mb-4">
                  <span class="input-group-addon"><i class="icon-lock"></i></span>
                  <input type="password" class="form-control" placeholder="密码" v-model="password" @keyup.enter="login()">
                </div>
                <div class="input-group mb-3 point" v-show="isShow">
                  <span>{{ pointMessage }}</span>
                </div>
                <div class="row">
                  <div class="col-6">
                    <button type="button" class="btn btn-primary px-4" @click="login()">登录</button>
                  </div>
                  <!--<div class="col-6 text-right">-->
                    <!--<button type="button" class="btn btn-link px-0">Forgot password?</button>-->
                  <!--</div>-->
                </div>
              </div>
            </div>
            <div class="card text-white bg-primary py-5 d-md-down-none" style="width:44%">
              <div class="card-body text-center">
                <div>
                  <h2>TAKIGEN管理系统</h2>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { login } from '../service/api'
  export default {
    name: 'Login',
    data () {
      return {
        isShow: false,
        name: '',
        password: '',
        pointMessage: ''
      }
    },
    methods: {
      async login () {
        if (this.name === '' || this.password === '') {
          this.isShow = true
          this.pointMessage = '用户名或密码不能为空'
        } else {
          const { err, data } = await login({ name: this.name, password: this.password })
          console.log('err: ' + err)
          console.log('data: ' + data)
          const { code, msg } = data
          if (code === 200) {
            localStorage.setItem('username', this.name)
            console.log('msg: ' + msg)
            this.$router.push({
              path: '/homepage'
            })
          } else {
            this.isShow = true
            this.pointMessage = data.msg
          }
        }
      }
    }
  }
</script>

<style>
  .point{
    color: #f20000;
  }
  .card-body div h2{
    margin-top: 3.5rem;
  }
</style>
